import Vue from 'vue'
import { $POST, $DELETE, $PUT, $GET, $UploadFile } from '__UTILS__/http_request/request-method.js'
import VueConfig, {preffix} from '__CONFIG__/vue-config.js'
const disposeResFunName = VueConfig[preffix + 'REQUEST_DISPOSE'].name //获取到处理返回结果函数名
const disposeResFun = Vue.prototype[disposeResFunName] // 获取到了处理函数

//获取分页文章
export function getArticlesByPage({search = '', page, limit = 6}) {
  const API = 'articles'
  return $GET(API, {page, search, limit}, disposeResFun)
}

//获取所有广告
export function getArticleById(id) {
  const API = 'articles/' + id
  return $GET(API, disposeResFun)
}

//获取功能性的所有文章
export function getArticleByFunctionlityId(id) {
  const API = 'functionalities/' + id + '/articles'
  return $GET(API, disposeResFun)
}

//获取功能性的所有文章
export function getArticleByTagId(id) {
  const API = 'tags/' + id + '/articles'
  return $GET(API, disposeResFun)
}
//获取功能性的所有文章
export function getArticleRankList(property, length) {
  let API = 'articles/' + property + '/rank_list'
  if(length) API += ('?length=' + length)
  return $GET(API, disposeResFun)
}

//成功浏览文章
export function lookArticle(id) {
  let API = 'articles/' + id + '/look_times'
  return $PUT(API, disposeResFun)
}

//发表文章
export function publishArticle({
  content, headline, outline, cover_url, tag_ids, functionality_id,
  user_id, access_token
}) {
  let API = 'articles'
  return $POST(API, {
    content, headline, outline, cover_url, tag_ids, functionality_id,
    user_id, access_token, authority: '1'
  }, disposeResFun)
}

//更新文章
export function updateArticle(id, {
  user_id, functionality_id, tag_ids, headline, 
  outline, cover_url, authority, content, access_token
}) {
  let API = 'articles' + '/' + id
  return $PUT(API, { 
    user_id, functionality_id, tag_ids, headline, 
    outline, cover_url, authority: '1', content, access_token
  }, disposeResFun)
}


//删除文章
export function destroy({id, user_id, access_token}) {
  let API = 'articles' + '/' + id
  return $DELETE(API, { user_id, access_token }, disposeResFun)
}


